package com.mianyang.zhangsan.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.common.util.OperationManHelper;
import com.mianyang.zhangsan.model.dto.SecApprovalDto;
import com.mianyang.zhangsan.model.entity.SecApproval;
import com.mianyang.zhangsan.service.ISecApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 审批服务
 * @author jiangjr
 * @since 2022-10-23
 */
@RestController
@RequestMapping("/operation")
public class SecApprovalController extends BaseController {
    @Autowired
    private ISecApprovalService approvalService;

    /**
     * 查询我的申请记录
     */
    @GetMapping("/apply")
    public R querySelfApply() {
        List<SecApproval> approvals = approvalService.selectSelfApply(OperationManHelper.getAccountId());
        return success("approvals", approvals);
    }

    /**
     * 查询我的审批记录
     */
    @GetMapping("/approval")
    public R querySelfApproval(@RequestParam(value = "senderName", required = false) String senderName,
                               @RequestParam(value = "approvalStatus", required = false) Integer approvalStatus,
                               @RequestParam(value = "current", defaultValue = "1") Integer current,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SecApproval> approvals = approvalService.selectSelfApproval(senderName, approvalStatus, current, pageSize);
        return success("approvals", approvals);
    }

    /**
     * 删除我的记录
     */
    @DeleteMapping("/approval/{approvalId}")
    public R deleteSelfApproval(@PathVariable("approvalId") Long approvalId) {
        approvalService.deleteSelfApproval(approvalId);
        return success();
    }

    /**
     * 审批发起的申请
     */
    @PostMapping("/decide")
    public R approval(@RequestBody SecApprovalDto approval) {
        approvalService.updateApproval(approval);
        return success();
    }

    /**
     * 发起审批
     */
    @PostMapping("/apply/send")
    public R sendApply(@RequestBody SecApprovalDto approval) {
        approvalService.sendApply(approval);
        return success();
    }
}
